package com.focusai.efairy.ui.activity.dev;

import android.content.Intent;
import android.graphics.BitmapFactory;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DeviceItem;
import com.focusai.efairy.ui.activity.location.AMapLocationActivity;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.dev.DevLocationAdapter;
import com.focusai.efairy.utils.StrByCodeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/8/31.
 */

public class DevMapLocationActivity extends AMapLocationActivity {
    private List<DeviceItem> mList;
    private DevLocationAdapter mAdapter;

    @Override
    protected void setDefaultValues() {
        super.setDefaultValues();
        setCenterTitleName("设备分布");
        if (getIntent() != null && getIntent().getExtras() != null) {
            mList = (List<DeviceItem>) getIntent().getExtras().getSerializable(KEY_MAP_ITEMS);
        }
        if (mList != null && mList.size() != 0) {
            mAdapter = new DevLocationAdapter(getContext());
            setAdapter(mAdapter);
            mAdapter.refreshData(mList);
            mAdapter.setOnItemViewClickListener(new DevLocationAdapter.OnItemViewClickListener() {
                @Override
                public void onItemClick(BaseRecyclerViewHolder holder, int position) {
                    if (markerList != null && markerList.size() > position && !markerList.get(position).isInfoWindowShown()) {
                        Marker marker = markerList.get(position);
                        aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker.getPosition())); //移动到对应位置
                        marker.showInfoWindow();
                    }
                }

                @Override
                public void onItemClickGoHere(DeviceItem item, int position) {
                    if (item != null){
                        showMapType(item.efairydevice_location_lat,item.efairydevice_location_lng,item.efairydevice_address);
                    }
                }
            });
        } else {
            showToast("设备列表暂无数据");
        }
    }

    @Override
    protected void addMarkersToMap() {
        ArrayList<MarkerOptions> markerOptionlst = new ArrayList<>();
        if (mList != null) {
            for (DeviceItem item : mList) {
                double dlong = item.efairydevice_location_lng;
                double dlat = item.efairydevice_location_lat;
                MarkerOptions markerOption = new MarkerOptions();
                markerOption.position(new LatLng(dlat, dlong));
                markerOption.title(item.efairydevice_name);
                markerOption.snippet(StrByCodeUtils.getStatusByDevState(item.efairydevice_alarm_id) +
                        "(" + StrByCodeUtils.getStatusByDevOnline(item.efairydevice_is_online) + ")"); //显示“故障(离线)”
                markerOption.draggable(true);
                int drawableId = R.drawable.icon_map_address_red;
                if (StrByCodeUtils.getColorByDevState(item.efairydevice_alarm_id) == R.color.dev_status_yellow){
                    drawableId = R.drawable.icon_map_address_yellow;
                }else if (StrByCodeUtils.getColorByDevState(item.efairydevice_alarm_id) == R.color.dev_status_green){
                    drawableId = R.drawable.icon_map_address_green;
                }
                markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), drawableId)));
                markerOptionlst.add(markerOption);
            }
            markerList = aMap.addMarkers(markerOptionlst, true);
            mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (markerList != null && markerList.size() > 0){
                        Marker marker = markerList.get(0);
                        aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker.getPosition())); //移动到对应位置
                        marker.showInfoWindow();
                    }
                }
            },1500);
        }
    }


    @Override
    public void onInfoWindowClick(Marker marker) {
        int position = markerList.indexOf(marker);
        if (position < mList.size()){
            DeviceItem deviceItem = mList.get(position);
            Intent intent = new Intent(getContext(), DeviceDetailInfoActivity.class);
            intent.putExtra(DeviceDetailInfoActivity.DEVICE_ITEM, deviceItem);
            startActivity(intent);
        }
        super.onInfoWindowClick(marker);
    }
}

